
import 'package:huxiaodui/api/model/unit_model.dart';
import 'package:huxiaodui/utils/global_controller.dart';
import 'package:amap_flutter_base/amap_flutter_base.dart';

import 'model_base.dart';

class AlarmModel extends ModelBase {
  static const List<String> typeList = [
    "无",
    "SOS报警",
    "低电报警",
    "设备拆除报警",
    "SOS报警",
    "跌倒报警",
    "落水报警",
    "靠近水域报警",
    "心率高报警",
    "血压高报警",
    "血压低报警",
    "血氧报警",
    "体温高报警"
  ];

  final UnitModel unit;
  final DateTime locationAt;
  final DateTime alarmAt;
  final bool expired;
  final Duration duration;
  final bool ring;
  final bool inform;
  final int scope;
  final int type;
  final int deviceType;
  final double? latitude;
  final double? longitude;
  late final LatLng? latLng;

  late Duration locationAtDistance;
  late String locationAtDistanceToString;
  late Duration alarmAtDistance;
  late String alarmAtDistanceToString;
  late String durationToString;

  // 构造函数，通过 Map 初始化
  AlarmModel({
    required super.id,
    required super.createdAt,
    required this.unit,
    required this.locationAt,
    required this.alarmAt,
    required this.expired,
    required this.duration,
    required this.ring,
    required this.inform,
    required this.scope,
    required this.type,
    required this.deviceType,
    this.latitude,
    this.longitude,
    this.latLng,
  }) {
    reload();
  }

  // 工厂构造函数，通过 JSON Map 创建 Alarm 实例
  factory AlarmModel.fromJson(Map<String, dynamic> map) {
    final duration = Duration(milliseconds: map["duration"] ?? 0);
    final locationAt = DateTime.fromMillisecondsSinceEpoch(map["locationAt"] ?? 0);
    final alarmAt = DateTime.fromMillisecondsSinceEpoch(map["alarmAt"] ?? 0);
    final latitude = map["latitude"] != null ? double.parse(map["latitude"].toString()) : null;
    final longitude = map["longitude"] != null ? double.parse(map["longitude"].toString()) : null;
    final latLng = (latitude != null && longitude != null && latitude != 0 && longitude != 0)
        ? LatLng(latitude, longitude)
        : null;

    return AlarmModel(
      id: map["id"] ?? -1,
      createdAt: DateTime.fromMillisecondsSinceEpoch(map["createdAt"] ?? 0),
      unit: UnitModel.fromJson(map["unit"] ?? {}),
      locationAt: locationAt,
      alarmAt: alarmAt,
      expired: map["expired"] ?? false,
      duration: duration,
      ring: map["ring"] ?? false,
      inform: map["inform"] ?? false,
      scope: map["scope"] ?? -1,
      type: map["type"] ?? 0,
      deviceType: map["deviceType"] ?? 0,
      latitude: latitude,
      longitude: longitude,
        latLng: latLng,
    );
  }

  // 将 Alarm 实例转换为 JSON Map
  Map<String, dynamic> toJson() {
    return {
      "id": id,
      "createdAt": createdAt.millisecondsSinceEpoch,
      "unit": unit.toJson(),
      "locationAt": locationAt.millisecondsSinceEpoch,
      "alarmAt": alarmAt.millisecondsSinceEpoch,
      "expired": expired,
      "duration": duration.inMilliseconds,
      "ring": ring,
      "inform": inform,
      "scope": scope,
      "type": type,
      "deviceType": deviceType,
      "latitude": latitude,
      "longitude": longitude,
    };
  }

  // 静态方法，将动态数据转换为 Alarm 列表
  static List<AlarmModel> toList(dynamic res) {
    var data = List<Map<String, dynamic>>.from(res);
    return data.map((map) => AlarmModel.fromJson(map)).toList();
  }

  // 更新对象状态的方法
  void reload() {
    durationToString = _formatDuration(duration);

    locationAtDistance = DateTime.now().difference(locationAt);
    locationAtDistanceToString = GlobalController.durationToString(locationAtDistance);

    alarmAtDistance = DateTime.now().difference(alarmAt);
    alarmAtDistanceToString = GlobalController.durationToString(alarmAtDistance);
  }

  // 私有方法，用于格式化 Duration 为字符串
  static String _formatDuration(Duration duration) {
    int days = duration.inDays;
    int hours = duration.inHours % 24;
    int minutes = duration.inMinutes % 60;
    int seconds = duration.inSeconds % 60;
    String result = "";
    if (days > 0) result += "$days天 ";
    if (hours > 0) result += "$hours小时 ";
    if (minutes > 0) result += "$minutes分钟 ";
    if (seconds > 0) result += "$seconds秒 ";
    return result;
  }
}
